package practice;


import com.thoughtworks.selenium.*;

import org.junit.AfterClass;
import org.openqa.selenium.By;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import java.io.File;
import jxl.*; 


public class LoginToHotmail extends SeleneseTestCase{
    
    @BeforeClass
    public void setUp() throws Exception {
        SeleniumServer seleniumserver=new SeleniumServer();
        seleniumserver.boot();
        seleniumserver.start();
        setUp("https://login.live.com/login.srf?wa=wsignin1.0&ct=1353209749&rver=6.1.6206.0&sa=1&ntprob=-1&wp=MBI_SSL_SHARED&wreply=https:%2F%2Fmail.live.com%2F%3Fowa%3D1%26owasuffix%3Dowa%252f&id=64855&snsc=1&cbcxt=mail/", "*firefox");
        selenium.open("/");
   	
        selenium.windowMaximize();
        selenium.windowFocus();
    }

    
    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("C:\\Users\\Alice\\Desktop\\eclipse\\samples\\TestData.xls",
                "Login", "Login");
        return(retObjArr);
    }
    
    @Test (dataProvider = "DP1")
    public void testLoginToHotmail(String Username, 
            String Password) throws Exception {   
    	
    	//int NumberOfRows = createData1().length;
    	//System.out.println(NumberOfRows);
    	selenium.type("login",Username);
    	selenium.type("passwd",Password);
		selenium.click("SI");
		 synchronized (selenium)
			{
		    	 selenium.wait(10000);
			}
		selenium.click("//*[@id='c_meun']");
		    synchronized (selenium)
			{
		    	selenium.wait(10000);
			}
		selenium.click("//*[@id='c_signout']");  
		
		
		 if(Username.equals("")){selenium.close();}
		 else{
			 setUp("https://login.live.com/login.srf?wa=wsignin1.0&ct=1353209749&rver=6.1.6206.0&sa=1&ntprob=-1&wp=MBI_SSL_SHARED&wreply=https:%2F%2Fmail.live.com%2F%3Fowa%3D1%26owasuffix%3Dowa%252f&id=64855&snsc=1&cbcxt=mail/", "*firefox");
	         selenium.open("/");
	    	
		 }
    	 }	        
    
    @AfterClass
    public void tearDown(){
        selenium.close();
        selenium.stop();
    } 
    
    public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
        

        return(tabArray);
    }
    
    
}//end of class
